package so.ifl.manager.staff;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.except.SessionTimeOutException;
import so.ifl.dao.StaffInfoDao;
import so.ifl.servcie.StaffService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.StaffDetailVo;
import so.ifl.vo.StaffVo;

/**
 *create by aaron.lu
 *create at 2013-3-10 上午9:15:06
 *<mailto: atyuan.lu@gmail.com>
 **/
@Component("staffManager")
public class StaffManager {
	
	@Autowired
	private StaffService staffService;
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 查询员工信息
	 * create by aaron.lu
	 * create at 2013-3-10 上午9:26:46
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getStaff(Page<StaffVo> page, Map<String, Object> params){
		logger_.debug("getStaff params:" + params);
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			params.put("company_id", accountVo.getCompanyId());
			
			int offset = page.getFirstEntityIndex();
			int limit = page.getPageSize();
			List<StaffVo> list = staffService.queryStaffWithParamAndPagination(params, offset, limit);
			Integer total = staffService.countStaffWithParam(params);
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("登录超时。。");
			
			page.setEntities(new ArrayList<StaffVo>());
			page.setEntityCount(0);
		}
		
	}
	
	/**
	 * 查询员工明细信息
	 * create by aaron.lu
	 * create at 2013-3-10 上午9:26:36
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getStaffDetail(Page<StaffDetailVo> page, Map<String, Object> params){
		logger_.debug("getStaffDetail params:" + params);
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		
		Integer staffId = (Integer)params.get("staff_id_");
		if (null != staffId) {
			//只查员工对应的
			int offset = page.getFirstEntityIndex();
			int limit = page.getPageSize();
			List<StaffDetailVo> list = staffService.queryStaffDetail(params, offset, limit);
			Integer total = list.size();
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("无员工ID查询条件。。");
			
			page.setEntities(new ArrayList<StaffDetailVo>());
			page.setEntityCount(0);
		}		
	}
	
	/**
	 * 保存员工信息
	 * create by aaron.lu
	 * create at 2013-3-10 上午9:30:43
	 * <mailto: atyuan.lu@gmail.com>
	 * @param staffVos
	 *
	 */
	@DataResolver
	public void saveStaff(Collection<StaffVo> staffVos){
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null != accountVo.getAccoutId()) {
			for (StaffVo staffVo : staffVos) {
				EntityState entityState = EntityUtils.getState(staffVo);
				if (EntityState.NEW.equals(entityState)) {
					// 新增
					logger_.debug("NEW:" + staffVo);
					staffVo.setCompnayId(accountVo.getCompanyId());//设置公司数据域
					staffService.insertStaff(staffVo);
				} else if (EntityState.MODIFIED.equals(entityState)) {
					// 修改
					logger_.debug("MODIFY:" + staffVo);
					staffService.updateStaff(staffVo);
				} else if (EntityState.DELETED.equals(entityState)) {
					// 删除
					logger_.debug("DELETE:" + staffVo);
					staffService.deleteStaff(staffVo.getStaffId());
				}
			}
		}else {
			throw new SessionTimeOutException("登录超时。。");
		}
		
	}
	
	/**
	 * 保存员工明细信息
	 * create by aaron.lu
	 * create at 2013-3-10 上午9:42:56
	 * <mailto: atyuan.lu@gmail.com>
	 * @param staffDetailVos
	 *
	 */
	@DataResolver
	public void saveStaffDetail(Collection<StaffDetailVo> staffDetailVos){
		for (StaffDetailVo staffDetailVo : staffDetailVos) {
			EntityState entityState = EntityUtils.getState(staffDetailVo);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.debug("NEW:" + staffDetailVo);
				staffService.insertStaffDetail(staffDetailVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.debug("MODIFY:" + staffDetailVo);
				staffService.updateStaffDetail(staffDetailVo);
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.debug("DELETE:" + staffDetailVo);
				staffService.deleteStaffDetail(staffDetailVo.getStaffDetailId());
			}
		}
	}
	
}
